/**
  * a client for timerserv.c
  * usage: timeclient hostname portnumber
  */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define oops(msg)	{perror(msg); exit(1);}

int main(int ac, char *av[]) {
	struct sockaddr_in servadd;			// the number to call
	struct hostent 	*hp;				// used to get number
	int sock_id, sock_fd;				// the socket and fd
	char message[BUFSIZ];				// to receive message
	int messlen;						// for message length

	// step1: get a socket
	sock_id = socket(AF_INET, SOCK_STREAM, 0);
	if (sock_id == -1) {
		oops("socket");
	}

	// step2: connect to server
	bzero(&servadd, sizeof(servadd));		// zero the address
	hp = gethostbyname(av[1]);
	if (hp == NULL) {
		oops(av[1]);
	}

	bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);

	servadd.sin_port = htons(atoi(av[2]));

	servadd.sin_family = AF_INET;

	if (connect(sock_id, (struct sockaddr *)&servadd, sizeof(servadd)) != 0) {
		oops("connect");
	}

	// step3: transfer data from server, then hang up
	messlen = read(sock_id, message, BUFSIZ);
	if (messlen == -1) {
		oops("read");
	}

	if (write(1, message, messlen) != messlen) {
		oops("read");
	}

	if (write(1, message, messlen) != messlen) {
		oops("write");
	}

	close(sock_id);
}
